﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace ConfExpert.Forms.Siparisler
{
    public partial class CustomerPerf : XForm
    {
        public CustomerPerf()
        {
            InitializeComponent();
        }

        List<string> yuzdeler = new List<string>();
        List<string> sezonlar = new List<string>();
        private void CustomerPerf_Load(object sender, EventArgs e)
        {
            gleTalepTipi.EditValue = 2;
            gridView1.Init("GetCustomerPerf", gleTalepTipi.EditValue.ToInt());

            gleTalepTipi.Edit.Assign(RI(Rp.TalepTipi));
            gleTalepTipi.EditValueChanged += gleTalepTipi_EditValueChanged;

            foreach (GridColumn c in gridView1.Columns)
                if (c.FieldName.EndsWith("Pct"))
                    yuzdeler.Add(c.FieldName);
                else
                    if (c.FieldName != "CustomerId")
                        sezonlar.Add(c.FieldName);

            GridFormatRule[] gridFormatRule = new GridFormatRule[yuzdeler.Count];
            FormatConditionRuleDataBar[] formatConditionRuleDataBar = new FormatConditionRuleDataBar[yuzdeler.Count];

            int i = 0;
            foreach (string col in yuzdeler)
            {
                /*
                gridFormatRule[i] = new GridFormatRule();
                formatConditionRuleDataBar[i] = new FormatConditionRuleDataBar();
                gridFormatRule[i].Column = gridView1.Columns[col];
                formatConditionRuleDataBar[i].PredefinedName = "Yellow Gradient";
                gridFormatRule[i].Rule = formatConditionRuleDataBar[i];
                */
                gridView1.SetCaptions(col + ":%" /*+ col.Replace("Pct", "%")*/);

                gridView1.Columns[col].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                gridView1.Columns[col].DisplayFormat.FormatString = "P2";
                i++;
            }

            gridView1.FormatAsNumber(0, sezonlar.ToArray());
            gridView1.AddSummary(sezonlar.ToArray());

            //gridView1.FormatRules.AddRange(gridFormatRule);
            gridView1.RowCellStyle += gridView1_RowCellStyle;

            gridView1.BestFitColumns();
        }

        void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "CustomerId")
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
            else if (yuzdeler.Contains(e.Column.FieldName))
                e.Appearance.ForeColor = Color.SlateGray;
        }

        void gleTalepTipi_EditValueChanged(object sender, EventArgs e)
        {
            gridView1.RefreshDataSource("GetCustomerPerf", gleTalepTipi.EditValue.ToInt());
            gridView1.BestFitColumns();

        }
    }
}
